package com.fanrenqiji.modules.sys.entity;

import com.fanrenqiji.common.utils.Collections3;
import com.fanrenqiji.common.utils.StringUtils;
import com.google.common.collect.Lists;
import com.fanrenqiji.common.persistence.DataEntity;

import org.hibernate.annotations.Type;
import org.hibernate.validator.constraints.Length;

import javax.persistence.*;
import java.util.List;

@Entity
@Table(name = "sys_notify")
public class Notify extends DataEntity {
	// 主键 订单编号
	private Long id;
	private String type; // 类型
	private String title; // 标题
	private String content; // 类型
	private String status; // 状态

	private Integer readNum; // 已读
	private Integer unReadNum; // 未读

	private boolean isSelf; // 是否只查询自己的通知

	private String readFlag; // 本人阅读状态

	private List<NotifyRecord> notifyRecords = Lists.newArrayList();

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Notify() {
		super();
	}

	@Column(name = "title")
	@Length(min = 0, max = 200, message = "标题长度必须介于 0 和 200 之间")
	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	@Column(name = "type")
	@Length(min = 0, max = 1, message = "类型长度必须介于 0 和 1 之间")
	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	@Column(name = "status")
	@Length(min = 0, max = 1, message = "状态长度必须介于 0 和 1 之间")
	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	@Column(name = "content")
	@Type(type="text")
	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	@Column(name = "read_num")
	public Integer getReadNum() {
		return readNum;
	}

	public void setReadNum(Integer readNum) {
		this.readNum = readNum;
	}

	@Column(name = "un_read_num")
	public Integer getUnReadNum() {
		return unReadNum;
	}

	public void setUnReadNum(Integer unReadNum) {
		this.unReadNum = unReadNum;
	}

	@OneToMany(mappedBy = "notify")
	public List<NotifyRecord> getNotifyRecords() {
		return notifyRecords;
	}

	public void setNotifyRecords(List<NotifyRecord> notifyRecords) {
		this.notifyRecords = notifyRecords;
	}

	/**
	 * 获取通知发送记录用户ID
	 * 
	 * @return
	 */
	@Transient
	public String getNotifyRecordIds() {
		return Collections3.extractToString(notifyRecords, "user.id", ",");

	}

	/**
	 * 设置通知发送记录用户ID
	 * 
	 * @return
	 */
	public void setNotifyRecordIds(String notifyRecord) {
		this.notifyRecords = Lists.newArrayList();
		for (String id : StringUtils.split(notifyRecord, ",")) {
			NotifyRecord entity = new NotifyRecord();
			entity.setNotify(this);
			entity.setUser(new User(Long.parseLong(id)));
			entity.setReadFlag("0");
			this.notifyRecords.add(entity);
		}
	}

	/**
	 * 获取通知发送记录用户Name
	 * 
	 * @return
	 */
	@Transient
	public String getNotifyRecordNames() {
		 return Collections3.extractToString(notifyRecords, "user.name",
		 ",");
	}

	/**
	 * 设置通知发送记录用户Name
	 * 
	 * @return
	 */
	public void setNotifyRecordNames(String notifyRecord) {
		// 什么也不做
	}

	@Transient
	public boolean isSelf() {
		return isSelf;
	}

	public void setSelf(boolean isSelf) {
		this.isSelf = isSelf;
	}

	@Transient
	public String getReadFlag() {
		return readFlag;
	}

	public void setReadFlag(String readFlag) {
		this.readFlag = readFlag;
	}
}
